package com.flame.user.service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import com.flame.core.utils.DateUtils;
import com.flame.user.controller.vo.UserAccountVo;
import com.flame.user.controller.vo.UserInfoVo;
import com.flame.user.controller.vo.UserRoleAddVo;
import com.flame.user.controller.vo.UserRoleVo;
import com.flame.user.entity.Account;
import com.flame.user.entity.SimpleUserInfo;
import com.flame.user.entity.UserInfo;
import com.flame.user.entity.UserRole;
import com.flame.user.exception.UserExceptionMessage;
import com.flame.user.exception.UserRuntimeException;
import com.flame.user.mapper.UserInfoMapper;
import com.flame.user.mapper.UserRoleMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

/**
 * flame_user_info 用户信息表
 *
 * @author 吴欣童
 * @since 2022-12-14 17:20:40
 */
@Service
public class UserInfoService {

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Autowired
    private AccountService accountService;

    @Autowired
    private UserRoleMapper userRoleMapper;

    public List<UserAccountVo> pageUserInfo(UserInfoVo userInfoVo) {
        return userInfoMapper.page(userInfoVo);
    }

    public void addUserInfo(UserInfo userInfo) {
        UserInfo param = userInfoMapper.findById(userInfo.getUserId());
        if (param != null) {
            throw new UserRuntimeException(UserExceptionMessage.USERINFO_EXIST);
        }
        // 1、创建账号
        Account account = new Account();
        account.setAccount(userInfo.getUserId());
        accountService.addAccount(account);

        // 2、创建用户信息
        userInfo.setCreateDate(DateUtils.currentDate());
        userInfoMapper.insert(userInfo);
    }

    public void updateUserInfo(UserInfo userInfo) {
        userInfoMapper.updateById(userInfo);
    }

    public void deleteUserInfo(UserInfo userInfo) {
        userInfoMapper.deleteById(userInfo.getUserId());
    }

    public void addUserRole(UserRoleAddVo userRoleAddVo) {
        userRoleMapper.deleteByUserId(userRoleAddVo.getUserId());
        if (!CollectionUtils.isEmpty(userRoleAddVo.getRoleCodeList())) {
            for (String roleCode : userRoleAddVo.getRoleCodeList()) {
                UserRole userRole = new UserRole();
                userRole.setUserId(userRoleAddVo.getUserId());
                userRole.setRoleCode(roleCode);
                userRoleMapper.insert(userRole);
            }
        }
    }

    public List<UserRole> pageUserRole(UserRoleVo userRoleVo) {
        return userRoleMapper.page(userRoleVo);
    }

    public UserInfo findById(String userId) {
        return userInfoMapper.findById(userId);
    }

    public List<SimpleUserInfo> simpleList(UserInfoVo userInfoVo) {
        List<UserAccountVo> userAccountVoList = userInfoMapper.page(userInfoVo);

        return userAccountVoList.stream().map(userAccountVo -> {
            SimpleUserInfo simpleUserInfo = new SimpleUserInfo();
            BeanUtils.copyProperties(userAccountVo, simpleUserInfo);
            return simpleUserInfo;
        }).collect(Collectors.toList());
    }

    public List<SimpleUserInfo> listSampleUserByIds(List<String> userIdList) {
        List<SimpleUserInfo> res = new ArrayList<>();
        for (String userId : userIdList) {
            UserInfo userInfo = userInfoMapper.findById(userId);
            SimpleUserInfo simpleUserInfo = new SimpleUserInfo();
            BeanUtils.copyProperties(userInfo, simpleUserInfo);
            res.add(simpleUserInfo);
        }
        return res;
    }
}
